Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
html-entities
Advanced tools
The html-entities package is a utility for encoding and decoding HTML entities. It can encode and decode a wide range of characters, including special characters, symbols, and emojis, to their corresponding HTML entities and vice versa. This is useful for preventing XSS attacks, rendering special characters in web pages, and working with text that includes characters that need to be escaped in HTML.
Encode special characters to HTML entities
This feature allows you to convert characters that have special meaning in HTML into their corresponding entities, making it safe to insert the text into HTML documents.
const { encode } = require('html-entities');
const result = encode('<div>Hello & Welcome!</div>');
console.log(result); // <div>Hello & Welcome!</div>
Decode HTML entities to their original characters
This feature enables you to convert HTML entities back into their original characters, which is useful when you need to process or display the text as it was originally intended.
const { decode } = require('html-entities');
const result = decode('<div>Hello & Welcome!</div>');
console.log(result); // <div>Hello & Welcome!</div>
Support for all HTML5 entities
The package includes support for all named HTML5 entities, allowing you to encode and decode a comprehensive set of characters.
const { encode } = require('html-entities');
const result = encode('© ∆');
console.log(result); // © Δ
Handling of non-ASCII characters
This feature is specifically for encoding non-ASCII characters into their numerical HTML entity equivalents, which can be important for internationalization and dealing with various character sets.
const { encodeNonAsciiHTML } = require('html-entities');
const result = encodeNonAsciiHTML('Привет!');
console.log(result); // Привет!
The 'he' package is an HTML entity encoder/decoder written in JavaScript. It is robust and handles a large number of character references. Compared to html-entities, 'he' claims to be the fastest and most comprehensive HTML entity library, and it strictly adheres to the HTML5 specification.
The 'entities' package is another library for encoding and decoding HTML entities. It is used internally by the 'htmlparser2' library, which is a fast and forgiving HTML/XML parser. While 'entities' offers similar functionality to html-entities, it is particularly optimized for use with 'htmlparser2' and may be more suitable for parsing tasks.
Fast html entities library.
$ npm install html-entities
XML entities
HTML validity and XSS attack prevention you can achieve from XmlEntities class.
const Entities = require('html-entities').XmlEntities;
const entities = new Entities();
console.log(entities.encode('<>"\'&©®')); // <>"'&©®
console.log(entities.encodeNonUTF('<>"\'&©®')); // <>"'&©®
console.log(entities.encodeNonASCII('<>"\'&©®')); // <>"\'&©®
console.log(entities.decode('<>"'&©®∆')); // <>"'&©®∆
All HTML entities encoding/decoding
const Entities = require('html-entities').AllHtmlEntities;
const entities = new Entities();
console.log(entities.encode('<>"&©®∆')); // <>"&©®∆
console.log(entities.encodeNonUTF('<>"&©®∆')); // <>"&©®∆
console.log(entities.encodeNonASCII('<>"&©®∆')); // <>"&©®∆
console.log(entities.decode('<>"&©®')); // <>"&©®
Available classes
const XmlEntities = require('html-entities').XmlEntities, // <>"'& + &#...; decoding
Html4Entities = require('html-entities').Html4Entities, // HTML4 entities.
Html5Entities = require('html-entities').Html5Entities, // HTML5 entities.
AllHtmlEntities = require('html-entities').AllHtmlEntities; // Synonym for HTML5 entities.
Supports four methods for every class:
FAQs
Fastest HTML entities encode/decode library.
The npm package html-entities receives a total of 17,233,917 weekly downloads. As such, html-entities popularity was classified as popular.
We found that html-entities demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.